<template>
  <el-container class="user-container">
    <!-- 头部 -->
    <el-header>
      <div class="header-left">
        <span class="header-title">书籍分享平台</span>
      </div>
      <div class="header-right">
        <template v-if="userStore.isLoggedIn">
          <el-button 
            type="text" 
            class="home-button" 
            @click="goToHome"
          >
            <el-icon><House /></el-icon>
            返回主页
          </el-button>

          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <div class="user-info">
                <img 
                  :src="userStore.userInfo?.avatar_data" 
                  class="user-avatar"
                  alt="用户头像"
                />
                <span>{{ userStore.nickname }}</span>
                <el-icon class="el-icon--right">
                  <arrow-down />
                </el-icon>
              </div>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="post">分享书籍</el-dropdown-item>
                <el-dropdown-item command="myPosts">已发布</el-dropdown-item>
                <el-dropdown-item command="drafts">草稿箱</el-dropdown-item>
                <el-dropdown-item command="myLikes">我的点赞</el-dropdown-item>
                <el-dropdown-item command="notifications">通知中心</el-dropdown-item>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
        <template v-else>
          <el-button type="primary" @click="goToLogin">登录/注册</el-button>
        </template>
      </div>
    </el-header>

    <!-- 主体区域 -->
    <el-container>
      <!-- 内容区域 -->
      <el-main class="scrollable-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ArrowDown, House } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

// 处理下拉菜单命令
const handleCommand = (command) => {
  switch (command) {
    case 'post':
      // 在新标签页打开
      const routeUrl = router.resolve({
        name: 'PostBook'
      })
      window.open(routeUrl.href, '_blank')
      break
    case 'myPosts':
      router.push('/user/my-posts')
      break
    case 'drafts':
      router.push('/user/drafts')
      break
    case 'myLikes':
      router.push('/user/my-likes')
      break
    case 'notifications':
      router.push('/user/notifications')
      break
    case 'profile':
      router.push('/user/profile')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/user/login')
}

// 退出登录
const handleLogout = () => {
  userStore.clearUserInfo()
  router.push('/user/login')
  ElMessage.success('已退出登录')
}

// 返回主页方法
const goToHome = () => {
  router.push('/user/home')
}
</script>

<style scoped>
.user-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.el-header {
  flex-shrink: 0;
  background-color: #fff;
  border-bottom: 1px solid #ddd;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-title {
  font-size: 16px;
  font-weight: 500;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid #e0e0e0;
}

.home-button {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  color: #409EFF;
}

.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
  color: #409EFF;
  font-size: 14px;
  line-height: 1;
}

.el-dropdown-link .el-icon {
  margin-left: 5px;
}

.el-container {
  flex: 1;
  display: flex;
  min-height: 0;
}

.el-main.scrollable-main {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background-color: #fff;
}
</style>